﻿/// <reference path='jquery.js' />
/// <reference path='jquery-vsdoc.js' />

// Public global variables

// Time period (in millisecs) the session timeout notification will be displayed for,
// before the session ends and the user is logged out
var NotificationTimeoutMs = 5 * 60 * 1000;
// Interval at which session heartbeat ajax calls will be performed
var SessionHeartbeatIntervalMs = 20 * 60 * 1000;
// URL of the logout page user will be tranferred to on session end
var LogoutPageUrl = AppRoot + 'Logout.aspx?message=' + SessionTimeoutMessage;
// URL of the page to be called using ajax to reset ASP.NET session timer
var SessionHeartbeatPageUrl = AppRoot + 'SessionHeartbeat/ResetSessionTimer.aspx';
// URL of the session timeout notification page to be displyed as a popup
var SessionTimeoutNotificationPageUrl = AppRoot + 'SessionHeartbeat/SessionTimeoutNotification.htm';

var SessionTimeoutNotificationWindowWidth = 300;
var SessionTimeoutNotificationWindowHeight = 200;

// Flag indicating if ASP.NET session timeout counter needs 
// to be reset during a session heartbeat call
var IsSessionHeartbeatNeeded = false;

// Local file variables

// Timer ID's
var _sessionTimerID;
var _notificationTimerID;
var _sessionHeartbeatIntervalID;
// Reference to the timeout notification popup window
var _sessionTimeoutNotificationWindow;

// Initializes session heartbeat for the page
// if it is enabled
function InitSessionHeartbeat()
{
    if (IsSessionHeartbeatEnabled)
    {
        SetupSessionHeartbeat();
    }
}

// Performs the setup of session heartbeat functionality for the page.
// If the page is a popup, all the session heartbeat execution needs
// to be handled by its parent opener page, so all the events will be sent to the
// opener insted of the popup page itself.
function SetupSessionHeartbeat()
{
    if (opener == null || opener.closed)
    {
        window.clearInterval(_sessionHeartbeatIntervalID);
        _sessionHeartbeatIntervalID = window.setInterval('PerformSessionHeartbeat(false)', SessionHeartbeatIntervalMs);
        ResetTimers();

        $('body').keypress(function()
        {
            IsSessionHeartbeatNeeded = true;
        });
    }
    else
    {
        opener.ResetTimers();
        opener.IsSessionHeartbeatNeeded = false;

        $('body').keypress(function()
        {
            opener.IsSessionHeartbeatNeeded = true;
        });
    }
}

function Logout()
{
    if (_sessionTimeoutNotificationWindow != null && !_sessionTimeoutNotificationWindow.closed)
    {
        _sessionTimeoutNotificationWindow.close();
    }

    window.location = LogoutPageUrl;
}

// Shows a popup with session timeout notification message
function ShowSessionTimeoutNotification()
{
    if (!IsSessionHeartbeatNeeded)
    {
        var left = (screen.width / 2) - (SessionTimeoutNotificationWindowWidth / 2);
        var top = (screen.height / 2) - (SessionTimeoutNotificationWindowHeight / 2);

        _sessionTimeoutNotificationWindow = window.open(SessionTimeoutNotificationPageUrl,
            'sessionTimeoutNotification',
            'height=' + SessionTimeoutNotificationWindowHeight + ',width=' + SessionTimeoutNotificationWindowWidth + ',status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=0,top=' + top + ',left=' + left,
            true);
    }
}

// Resets all the session heartbeat related timers 
function ResetTimers()
{
    window.clearTimeout(_sessionTimerID);
    _sessionTimerID = window.setTimeout('PerformSessionHeartbeat(true)', SessionTimeoutMs);

    window.clearTimeout(_notificationTimerID);
    _notificationTimerID = window.setTimeout('ShowSessionTimeoutNotification()', SessionTimeoutMs - NotificationTimeoutMs);
}

// Checks if the flag is set to perform a session heartbeat and 
// if it is, performs an ajax call to reset ASP>NET session timeout counter
function PerformSessionHeartbeat(logoutIfHeartbeatNotNeeded)
{
    if (IsSessionHeartbeatNeeded)
    {
        $.post(SessionHeartbeatPageUrl, null, function()
        {
            IsSessionHeartbeatNeeded = false;
            ResetTimers();
        });
    }
    else if (logoutIfHeartbeatNotNeeded)
    {
        Logout();
    }
}

// Performs session heartbeat ajax call no matter if the flag
// indicating whether the call is needed or not is set to true or false.
function ForcePerformSessionHeartbeat()
{
    IsSessionHeartbeatNeeded = true;
    PerformSessionHeartbeat(false);
}
